import { add } from '@/util'
import { createStore } from 'vuex'
import {BigNumber} from 'bignumber.js'

// 创建一个新的 store 实例
const store = createStore({
  state () {
    return {     
      count: 0,
      size:-1,
      msg:'',
      todos:[
        {id:1,text:'aaa',done:true},
        {id:2,text:'www',done:false},
      ],
      user_info:{
        phones:'',
        password:''
      },
      sales:[{
        name:'',
        img:'',
        sale:'',
        qisong:'',
        basePrice:'',
        info:''
      }],
      list:[{
        id:'1',
        cardName:'沃尔玛',
        cardState:'已取消',
        cardImg:[require('@/assets/eggplant.jpeg'),require('@/assets/crayfish.jpeg'),require('@/assets/tamato.jpeg')],
        cardMonney:33.3,
        cardTotal:3
      },{
        id:'2',
        cardName:'世纪联华',
        cardState:'已完成',
        cardImg:[require('@/assets/tamato.jpeg'),require('@/assets/liuliumei.jpg'),require('@/assets/water.jpeg'),require('@/assets/patato chips.jpeg'),require('@/assets/beef.jpeg')],
        cardMonney:88.8,
        cardTotal:4
      }],
      saleInfo:[{
        id:'1',
        name:'沃尔玛',
        img:require("@/assets/walmart.png"),
        sale:'2万+',
        qisong:'0',
        basePrice:'5',
        info:'VIP尊享89元减4元运费券(每月3张)',
      
      },
      {
        id:'2',
        name:'山姆会员超市',
        img:require("@/assets/samclub.jpeg"),
        sale:'2千+',
        qisong:'10',
        basePrice:'20',
        info:'联合利华洗护满10减5',
        
      },
      {
        id:'3',
        name:'福泰隆',
        img:require("@/assets/Futailong.jpeg"),
        sale:'5千+',
        qisong:'0',
        basePrice:'10',
        info:'全场满100减20',
       
      },
      {
        id:'4',
        name:'世纪联华',
        img:require("@/assets/centurymart.jpeg"),
        sale:'4千+',
        qisong:'10',
        basePrice:'20',
        info:'联合利华洗护满10减5',
        isClick:false
      }
      
      ],
      address1:[{
        id:'',
        name:'',
        phone:'',
        city:'',
        school:'',
        number:'',
        select:false
      }],
      address:[{
         id:"1",
         name:'小穆',
         phone:'18767584568',
         city:'北京市海淀区西三环北路',
         school:'北京理工大学',
         number:'国防科技园2号楼 10层',
         select:true
      },{
         id:"2",
         name:'小五',
         phone:'18767584568',
         city:'北京市海淀区西三环北路',
         school:'北京理工大学',
         number:'国防科技园2号楼 10层',
         select:false
      }],
      goods:[{
          img:require("@/assets/water.jpeg"),
          goodsName:"饮用水600ml/瓶",
          goodsNumber:"50",
          price:"1.50",
          kg:2.8,
          oldPrice:"2.00",
          amount:0,
          isSelect:false,
          category:"miaosha"
      },{
          img:require("@/assets/liuliumei.jpg"),
          goodsName:"溜溜梅500g/包",
          goodsNumber:"50",
          price:"4.50",
          kg:2.1,
          oldPrice:"6.00",
          amount:0,
          isSelect:false,
          category:"miaosha"
      },{
          img:require("@/assets/tamato.jpeg"),
          goodsName:"番茄250g/份",
          goodsNumber:"10",
          price:"33.6",
          oldPrice:"35.6",
          amount:0,
          kg:2.3,
          isSelect:false,
          category:"fruit"
      },{
        img:require("@/assets/patato chips.jpeg"),
        goodsName:"薯片250g/包",
        goodsNumber:"100",
        price:"4.50",
        kg:3.2,
        oldPrice:"6.50",
        amount:0,
        isSelect:false,
        category:"food"
      },{
          img:require("@/assets/eggplant.jpeg"),
          goodsName:"茄子250g/份",
          goodsNumber:"50",
          price:"16.9",
          oldPrice:"25.6",
          kg:2.3,
          amount:0,
          isSelect:false,
          category:"shucai"
      },{
        img:require("@/assets/crayfish.jpeg"),
        goodsName:"小龙虾250g/份",
        goodsNumber:"15",
        price:"45.5",
        kg:1.5,
        oldPrice:"57.9",
        amount:0,
        isSelect:false,
        category:"meat"
      },{
          img:require("@/assets/beef.jpeg"),
          goodsName:"牛肉250g/份",
          goodsNumber:"15",
          price:"55.7",
          kg:2.5,
          oldPrice:"67.9",
          amount:0,
          isSelect:false,
          category:"meat"
      }],
      total:0,
      amount1:0,
      cartShow:false,
      cart:[],
      pays:false,
      tijiao:false,
      dizhi:''
    }
    
  },
  mutations: {
    //异步操作不能用mutations 如：网络请求 定时器
    increment (state,n) {
      state.count+=n
    },
    change(state,n){
      if (state.todos[n].done === true) {
        state.todos[n].done = false
      } else {
        state.todos[n].done =true
      }
    },
    info(state,user_info){
      state.user_info=user_info
    },
    // saleInfo(state,saleInfo){
    //   state.saleInfo=saleInfo
    // },
    sales(state,sales){
      state.sales=sales
    },
    changeName(state,msg){
      state.msg=msg
    },
    address1(state,address1){
      state.address1=address1
    },
    address(state,address1){
      state.address.splice(0,address1)
    },
    jia(state,n) {
      state.goods[n].amount+=1
      state.amount1+=1
      // state.total+=(state.goods[n].price)*1
      // console.log(state.total)

     var a=BigNumber(state.total)
     var b=BigNumber(state.goods[n].price)
     state.total=a.plus(b).toNumber()
     console.log(b)
     console.log(a)

     
      state.goods[n].isSelect=true
      // console.log(state.goods[n].isSelect+"选中")
      if(state.goods[n].amount===1){
        state.cart.push(state.goods[n])
      }
      
      console.log(state.cart)
    },
    jian(state,n){
       state.goods[n].amount-=1
       state.amount1-=1
      //  console.log(state.total)
        var a=BigNumber(state.total)
       var b=BigNumber(state.goods[n].price)
        state.total=a.minus(b).toNumber()
        console.log(b)
        console.log(a)

      if(state.goods[n].amount<=0){
        state.cart.splice(n,1)
        state.goods[n].amount=0
        state.goods[n].isSelect=false
      }
      console.log(state.cart)
    },
    
    showGoods(state){
      state.cartShow=!state.cartShow
    },
    addCart(state,cart){
      state.cart=cart
    }
  },
  getters:{
    doneTodos: (state) => {
      return state.todos.filter(todo => todo.done===true)
    },
    notTodos:(state) => {
      return state.todos.filter(item => !item.done)
    },
    Info:(state)=>{
      return state.saleInfo
    },
    address:(state)=>{
      return state.address
    },
    goods0:(state)=>{
      return state.goods
    },
    goods1:(state) => {
      return state.goods.filter(goods => goods.category==="miaosha")
    },
    goods2:(state) => {
      return state.goods.filter(goods => goods.category==="fruit")
    },
    goods3:(state) => {
      return state.goods.filter(goods => goods.category==="food")
    },
    goods4:(state) => {
      return state.goods.filter(goods => goods.category==="shucai")
    },
    goods5:(state) => {
      return state.goods.filter(goods => goods.category==="meat")
    },
    amount:(state)=>{
      return state.goods.amount
    },
    selectGoods:(state)=>{
      return state.goods.filter(goods => goods.isSelect===true)
    },
    selectAddress:(state)=>{
      return state.address.filter(address=>address.select===true)
    }
  }
})

export{
    store
}